package com.tang.services.impl;

import com.tang.bean.Book;
import com.tang.bean.Page;
import com.tang.dao.BookDao;
import com.tang.dao.impl.BookDaoImpl;
import com.tang.services.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {
  private BookDao bookDao=new BookDaoImpl();

  @Override
  public List<Book> getBookList() throws Exception{
    return bookDao.selectBookList();
  }

  @Override
  public void removeBook(Integer bookId) throws Exception {
    bookDao.deleteBook(bookId);
  }


  @Override
  public void saveOrUpdateBook(Book book) throws Exception {
    //1. 判断book中的bookId是否为空
    if (book.getBookId() == null || "".equals(book.getBookId()) || book.getBookId() == 0) {
      //说明bookId为空,那么就是添加图书信息
      // 固定设置图书的图片路径
      book.setImgPath("static/uploads/jiaofu.jpg");
      //调用持久层的方法进行添加
      bookDao.insertBook(book);
    } else {
      //说明bookId不为空，那么就是修改图书信息
      // 调用持久层的方法进行修改
      bookDao.updateBook(book);
    }
  }

  @Override
  public Book getBookById (Integer bookId) throws Exception {
    return bookDao.selectBookByPrimaryKey(bookId);
  }

  @Override
  public Page<Book> getPage(Integer pageNo) throws Exception {
    Page<Book> page=new Page<Book>();
    page.setPageNo(pageNo);
    page.setMaxPageSize(20);

    //总记录数
    Integer bookCount=bookDao.getBookCount();
    page.setItemsCount(bookCount);

    //总页码
    Integer pageTotal =(bookCount%page.getMaxPageSize())>0 ? bookCount / page.getMaxPageSize() + 1 :bookCount / page.getMaxPageSize();
    page.setPageTotal(pageTotal);

    //计算当前页开始的记录数
    Integer pageBegin=(pageNo-1)* page.getMaxPageSize();
    List<Book> pageItems=bookDao.getPageItems(pageBegin,page.getMaxPageSize());
    page.setPageItems(pageItems);

    return page;
  }

  @Override
  public Page<Book> pageByPrice(Integer pageNo, Integer min, Integer max) throws Exception {
    Page<Book> page=new Page<Book>();
    page.setPageNo(pageNo);
    page.setMaxPageSize(20);
    //总记录数
    Integer bookCount=bookDao.getBookCountByPrice(min,max);
    page.setItemsCount(bookCount);

    //总页码
    Integer pageTotal =(bookCount%page.getMaxPageSize())>0 ? bookCount / page.getMaxPageSize() + 1 :bookCount / page.getMaxPageSize();
    page.setPageTotal(pageTotal);

    //计算当前页开始的记录数
    Integer pageBegin=(pageNo-1)* page.getMaxPageSize();
    List<Book> pageItems=bookDao.getPageItemsByPrice(pageBegin,page.getMaxPageSize(),min,max);
    page.setPageItems(pageItems);

    return page;
  }

  @Override
  public Page<Book> pageByType(Integer pageNo, String typeId) throws Exception {
    Page<Book> page=new Page<Book>();
    page.setPageNo(pageNo);
    page.setMaxPageSize(20);
    //总记录数
    Integer bookCount=bookDao.getBookCountByType(typeId);
    page.setItemsCount(bookCount);
    //总页码
    Integer pageTotal =(bookCount%page.getMaxPageSize())>0 ? bookCount / page.getMaxPageSize() + 1 :bookCount / page.getMaxPageSize();
    page.setPageTotal(pageTotal);

    //计算当前页开始的记录数
    Integer pageBegin=(pageNo-1)* page.getMaxPageSize();
    List<Book> pageItems=bookDao.getPageItemsByType(pageBegin,page.getMaxPageSize(),typeId);
    page.setPageItems(pageItems);

    return page;
  }

  @Override
  public Page<Book> pageByKey(Integer pageNo, String key) throws Exception{
    Page<Book> page=new Page<Book>();
    page.setPageNo(pageNo);
    page.setMaxPageSize(20);
    //总记录数
    Integer bookCount=bookDao.getBookCountByKey(key);
    page.setItemsCount(bookCount);

    //总页码
    Integer pageTotal =(bookCount%page.getMaxPageSize())>0 ? bookCount / page.getMaxPageSize() + 1 :bookCount / page.getMaxPageSize();
    page.setPageTotal(pageTotal);

    //计算当前页开始的记录数
    Integer pageBegin=(pageNo-1)* page.getMaxPageSize();
    List<Book> pageItems=bookDao.getPageItemsByKey(pageBegin,page.getMaxPageSize(),key);
    page.setPageItems(pageItems);

    return page;
  }

}
